<?php
namespace app\service;
use Grafika\Grafika;

/**
 * Created by PhpStorm.
 * User: kphcdr
 * Date: 2018/4/12
 * Time: 下午5:28
 */
class ImageProcessing
{
    public function index()
    {
        dump('处理开始'. microtime(true));
        $bg = APP .'asset/bg.png';
        $header_remote = 'http://kphcdr.qiniudn.com/540.jpg?roundPic/radius/!50p';


        $card = APP . 'asset/card.jpg';

        $name1 = $this->handle($bg,$header_remote,true,22,40);
        $name2 = $this->handle($name1,$card,false,22,40);


        dump('处理结束'. microtime(true));

    }

    /**
     * 把$image 放到 $bg 上
     * @param      $bg
     * @param      $image
     * @param bool $isRemote
     *
     * @return string
     */
    public function handle($bg,$image,$isRemote=true,$left,$top)
    {
        $name = PPPHP .'/storage/'.time().rand(1,999999).'.png';

        $editor = Grafika::createEditor();
        $editor->open($image1,$bg);


        if($isRemote) {
            file_put_contents($name,file_get_contents($image));
            $editor->open($image2,$name);
        } else {
            $editor->open($image2,$image);
        }

        $editor->blend($image1,$image2,'normal',1.0,'top-left',$left,$top);
        $editor->save($image1,$name);

        return $name;
    }
}